<!--
 * @Author: jhw jiaxianshengye@gmail.com
 * @LastEditTime: 2025-08-15 10:45:02
 * @LastEditors: jhw
 * @Description: 文件描述
-->
<template>
  <div>
    <div class="containers">
      <!-- 分为三个部分 -->
      <div class="left">
        <el-button :icon="ArrowLeft" circle size="small" @click="goHome" />
      </div>
      <div class="center">
        <div v-if="isShowBtn">
          <el-button v-if="!id" type="danger" size="small" @click="reset">重置问卷</el-button>
          <el-button v-if="!id" type="success" size="small" @click="handleSaveSurvey"
            >保存问卷</el-button
          >
          <el-button
            v-if="id && type !== 'detail'"
            type="warning"
            size="small"
            @click="handleUpdateSurvey"
            >更新问卷</el-button
          >
        </div>
        <div v-if="isShowBtn">
          <el-button type="primary" size="small" @click="goPreview">预览</el-button>
        </div>
      </div>
      <div class="right">
        <el-avatar :size="30" :src="logoGif" />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ArrowLeft } from '@element-plus/icons-vue';
import { useRoute, useRouter } from 'vue-router';
import logoGif from '@/assets/logo.gif';
import { ElMessage, ElMessageBox } from 'element-plus';
import { useEditorStore } from '@/stores/useEditor';
import { computed } from 'vue';
import { removeComsType } from '@/utils';
import { saveSurvey, updateSurvey } from '@/api';
const store = useEditorStore();

const router = useRouter();
const route = useRoute();
const id = computed(() => route.query.id as string);
const type = route.query.type as string; // 问卷类型

const goHome = () => {
  localStorage.setItem('activeView', 'home');
  router.push('/');
};

const goPreview = () => {
  ElMessageBox.confirm('预览会保存当前问卷，是否确定预览？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(async () => {
      const coms = removeComsType(JSON.parse(JSON.stringify(store.coms)));
      const surveyToSave = {
        id: type === 'edit' ? id.value : undefined,
        title: store.getSurveyTitle,
        surveyCount: store.questionCount,
        components: JSON.parse(JSON.stringify(coms)),
      };
      const { code, data } =
        type === 'edit' ? await updateSurvey(surveyToSave) : await saveSurvey(surveyToSave);
      if (code === 200) {
        router.push({
          name: 'preview',
          query: {
            id: type === 'edit' ? id.value : data.id,
            from: 'edit',
          },
        });
      }
    })
    .catch(() => {});
};

const reset = () => {
  ElMessageBox.confirm('是否确定重置试卷？已有题目将全部删除', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      store.resetComs();
      ElMessage({
        type: 'success',
        message: '已重置',
      });
    })
    .catch(() => {});
};

const handleSaveSurvey = () => {
  ElMessageBox.alert('是否确定保存问卷？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'info',
  })
    .then(async () => {
      const coms = removeComsType(JSON.parse(JSON.stringify(store.coms)));
      const surveyToSave = {
        title: store.getSurveyTitle,
        surveyCount: store.questionCount,
        components: JSON.parse(JSON.stringify(coms)),
      };
      const { code, data } = await saveSurvey(surveyToSave);
      if (code === 200) {
        ElMessage.success('保存成功');
        router.push({
          name: 'base-component',
          query: {
            id: data.id,
          },
        });
      }
    })
    .catch(() => {});
};

const handleUpdateSurvey = () => {
  ElMessageBox.alert('是否确定更新问卷？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'info',
  })
    .then(async () => {
      const coms = removeComsType(JSON.parse(JSON.stringify(store.coms)));
      const surveyToSave = {
        id: id.value,
        title: store.getSurveyTitle,
        surveyCount: store.questionCount,
        components: JSON.parse(JSON.stringify(coms)),
      };
      const { code } = await updateSurvey(surveyToSave);
      if (code === 200) {
        ElMessage.success('更新成功');
      }
    })
    .catch(() => {});
};

const isShowBtn = computed(() => {
  return route.name === 'base-component' || route.name === 'enhance-component';
});
</script>

<style scoped lang="scss">
.containers {
  @apply flex w-full h-50px items-center self-start;
  border-bottom: 1px solid var(--border-color);

  .left {
    @apply flex justify-center items-center w-60px h-full;
  }
  .center {
    @apply flex flex-1 h-full items-center justify-between pl-15px pr-15px;
    border-left: 1px solid var(--border-color);
    border-right: 1px solid var(--border-color);
  }
  .right {
    @apply w-80px h-full flex justify-center items-center;
  }
}
</style>
